package com.example.android.utils.viewutils;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import androidx.navigation.Navigation;

import com.example.android.R;
import com.example.android.bean.CarOrder;
import com.example.android.bean.CarRelease;
import com.example.android.utils.BaseModel;
import com.example.android.utils.NetUtil;
import com.example.android.utils.api.CarOrderApi;
import com.google.android.material.snackbar.Snackbar;

import java.util.Calendar;

import razerdp.basepopup.BasePopupWindow;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ReservePopup extends BasePopupWindow {
    public ReservePopup(Context context, CarRelease carRelease) {
        super(context);
        Log.i("TAG", "ReservePopupRelease: "+carRelease.getUser().toString());

        setContentView(createPopupById(R.layout.reserve_car));
        View contentView = getContentView();
        DatePicker datePicker = contentView.findViewById(R.id.date_picker_actions);
        EditText phone = contentView.findViewById(R.id.phoneNumber);
        phone.setText(carRelease.getUser().getPhonenumber());
        Button button = contentView.findViewById(R.id.button4);
        CarOrder carOrder = new CarOrder();
        Calendar calendar=Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        carOrder.setUseTime(year+"-"+month+"-"+day+" 01:00");
        datePicker.init(year, month, day, (datePicker1, i, i1, i2) -> {
            String date=i+"-"+i1+"-"+i2+" 01:00";
            carOrder.setUseTime(date);
        });



        carOrder.setReleaseId(carRelease.getId());
        button.setOnClickListener(view -> {

            carOrder.setTelePhone(phone.getText().toString());
            Log.i("TAG", "ReservePopup: "+carOrder.toString());
            NetUtil.INSTANCE.get(NetUtil.INSTANCE.getBaseUrlDev())
                    .create(CarOrderApi.class)
                    .saveOrder(carOrder)
                    .enqueue(new Callback<BaseModel<Integer>>() {
                @Override
                public void onResponse(Call<BaseModel<Integer>> call, Response<BaseModel<Integer>> response) {
                    Log.i("TAG", "onResponse: "+response.body().toString());

                    Snackbar.make(view, "預約成功：" + response.body().getMsg(), Snackbar.LENGTH_LONG).show();

                }

                @Override
                public void onFailure(Call<BaseModel<Integer>> call, Throwable t) {
                    Log.i("TAG", "onFailure: "+t.getMessage());
                    Snackbar.make(view, "預約失敗：" + t.getMessage(), Snackbar.LENGTH_LONG).show();

                }
            });
        });


    }
}
